import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);
const routes= [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/',
        component: () => import('../components/common/Home.vue'),
        meta: { title: '自述文件' },
        children: [
            {
                path: '/user',
                component: () => import('../components/page/Users.vue'),
                meta: { title: '后台用户' }
            },
            {
                path: '/customer',
                    component: () => import('../components/page/Customer.vue'),
                meta: { title: '用户管理' }
            },
            {
                path: '/permission',
                component: () => import('../components/page/Role.vue'),
                meta: { title: '权限管理' }
            },
            {
                path: '/news',
                component: () => import('../components/page/News.vue'),
                meta: { title: '帖子管理' }
            },
            {
                path: '/columns',
                    component: () => import('../components/page/Columns.vue'),
                meta: { title: '栏目管理' }
            },
            {
                path: '/topictype',
                    component: () => import('../components/page/Topictype.vue'),
                meta: { title: '话题分类' }
            },
            {
                path: '/topic',
                    component: () => import('../components/page/Topic.vue'),
                meta: { title: '话题管理' }
            },
            {
                path: '/tag',
                    component: () => import('../components/page/Tag.vue'),
                meta: { title: '话题分类' }
            },
            {
            path: '/like',
                component: () => import('../components/page/Like.vue'),
            meta: { title: '喜好管理' }
            },
            {
                path: '/hotword',
                    component: () => import('../components/page/Hotword.vue'),
                meta: { title: '热词管理' }
            },
            {
                path: '/advise',
                    component: () => import('../components/page/Advise.vue'),
                meta: { title: '意见反馈' }
            },
            {
                path: '/gold',
                    component: () => import('../components/page/Gold.vue'),
                meta: { title: '金币套餐' }
            },
            {
                path: '/vip',
                    component: () => import('../components/page/Vip.vue'),
                meta: { title: 'VIP套餐' }
            },
            {
                path: '/creation',
                    component: () => import('../components/page/Creation.vue'),
                meta: { title: '创作技巧' }
            }
        ]
    },
    {
        path: '/login',
        component: () => import('../components/page/Login.vue'),
        meta: { title: '登录' }
    }
];
const router = new VueRouter({
    mode: 'history',
    routes
});

//钩子函数，拦截所有routes中的请求
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 后台管理系统`;
    //判断token
    if (localStorage.getItem('token')!=null || to.fullPath.endsWith('login')) {
        next()
    } else {
        // 没登录则跳转到登录界面
        next('/login')
    }
});
export default router;
